package com.yanqu.road.server.manager.activity.springunionbanquet;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSpringUnionBanquetActivityMgr extends TempCrossMgr {

    private static Logger logger = LogManager.getLogger(CrossSpringUnionBanquetActivityMgr.class.getName());

    private static Map<Integer, CrossSpringUnionBanquetActivity> activityMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        Map<Integer, ActivityInfo> map = ActivityMgr.getOpenCrossActivityMapByType(eActivityType.SpringUnionBanquet.getValue());
        for (Map.Entry<Integer, ActivityInfo> entry : map.entrySet()) {
            ActivityInfo activityInfo = entry.getValue();
            CrossSpringUnionBanquetActivity activity = new CrossSpringUnionBanquetActivity(activityInfo);
            activity.init();
            activityMap.put(activityInfo.getActivityId(), activity);
        }

        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        for (Map.Entry<Integer, CrossSpringUnionBanquetActivity> entry : activityMap.entrySet()) {
            entry.getValue().save();
        }
        return true;
    }

    public static synchronized CrossSpringUnionBanquetActivity getActivity(int activityId) {
        if (!activityMap.containsKey(activityId)) {
            // 读配置 获取活动信息 保证有活动信息
            ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
            if (activityInfo == null) {
                return null;
            }
            // 不在展示期也不处理了
            if (!ActivityHelper.activityInShowTime(activityInfo)) {
                return null;
            }
            CrossSpringUnionBanquetActivity activity = new CrossSpringUnionBanquetActivity(activityInfo);
            activity.init();
            activityMap.put(activityId, activity);
        }
        return activityMap.get(activityId);
    }


    @Override
    public boolean removeExpireData() throws Exception {
        return true;
    }
}
